import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OrdinalEncoder


def preprocess_data(filepath, inuse_features, id_label, y_label):
    df = pd.read_csv(filepath)
    ids = df[id_label].values
    y = None
    if y_label in df.columns:
        y = df[y_label].values
        df = df.drop(y_label, axis=1)
    df = df.drop(inuse_features, axis=1)

    # 自动识别分类列和数值列
    categorical_cols = df.select_dtypes(include=['object']).columns
    numeric_cols = df.select_dtypes(include=[np.number]).columns

    # 定义分类列的处理步骤
    categorical_transformer = Pipeline(steps=[
        ('imputer', SimpleImputer(strategy='most_frequent')),
        ('encoder', OrdinalEncoder())
    ])

    # 定义数值列的处理步骤
    numeric_transformer = Pipeline(steps=[
        ('imputer', SimpleImputer(strategy='mean')),
        ('scaler', StandardScaler())
    ])

    # 创建 ColumnTransformer
    preprocessor = ColumnTransformer(
        transformers=[
            ('num', numeric_transformer, numeric_cols),
            ('cat', categorical_transformer, categorical_cols)
        ])

    # 预处理数据
    transformed_data = preprocessor.fit_transform(df)
    # 将处理后的数据转换为 DataFrame，并设置列名和索引
    x = pd.DataFrame(transformed_data, columns=df.columns, index=df.index).values
    return ids, x, y
